package cn.elead.chaos.remote.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import cn.elead.chaos.core.constant.ModuleCons;
import cn.elead.chaos.framework.properties.ChaosProperties;
import cn.elead.chaos.framework.properties.Sso;
import cn.elead.chaos.sso.core.constant.SsoConstant;
import cn.elead.chaos.sso.core.filter.SsoOauthFilter;

/**
 * 
 * @author luopeng
 *
 */
@Configuration
//表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
//@EnumScan("cn.elead.chaos.sso.server.enums")
@MapperScan(AutoConfiguration.BASE_DAO_PACKAGE)
@ComponentScans({ @ComponentScan(ModuleCons.CHAOS_PACKAGE) })
public class AutoConfiguration implements WebMvcConfigurer {

	public static final String BASE_PACKAGE = ModuleCons.CHAOS_PACKAGE + ".remote";

	public static final String BASE_DAO_PACKAGE = BASE_PACKAGE + ".**.mapper";

	@Autowired
	private ChaosProperties chaos;

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		/** 本地文件上传路径 */
		String uploadPath = chaos.getPath().getFilePath();
		registry.addResourceHandler(chaos.getPath().getResourcePath() + "/**").addResourceLocations("file:" + chaos.getPath().getFilePath() + "/");
		// 图片
		String resourcePrefix = chaos.getPath().getResourcePrefix();
		String image = chaos.getPath().getPrefix().getImage();
		registry.addResourceHandler(resourcePrefix + image + "/**").addResourceLocations("file:" + uploadPath + image);
		// 视频
		String video = chaos.getPath().getPrefix().getVideo();
		registry.addResourceHandler(resourcePrefix + video + "/**").addResourceLocations("file:" + uploadPath + video);
		// 视频
		String hls = chaos.getPath().getPrefix().getHls();
		registry.addResourceHandler(resourcePrefix + hls + "/**").addResourceLocations("file:" + uploadPath + hls);
	}

	@Bean
	public FilterRegistrationBean<SsoOauthFilter> SsoFilterRegistration() {

		FilterRegistrationBean<SsoOauthFilter> registration = new FilterRegistrationBean<SsoOauthFilter>();

		registration.setName("SsoOauthFilter");
		registration.setOrder(1);
		registration.addUrlPatterns("/*");

		SsoOauthFilter ssoOauthFilter = new SsoOauthFilter();
		// ssoOauthFilter.setLoginSuccessHandler(loginSuccessHandler());
		registration.setFilter(ssoOauthFilter);

		Sso sso = chaos.getSso();
		registration.addInitParameter(SsoConstant.SSO_SERVER, sso.getServer()); // 服务器 地址
		registration.addInitParameter(SsoConstant.PARAM_CLIENT_ID, sso.getClientId()); // 客户端ID
		registration.addInitParameter(SsoConstant.SSO_CLIENT_SECRET, sso.getClientSecret()); // 客户端秘钥 ,通过code换取token使用
		registration.addInitParameter(SsoConstant.CODE_CALL_BACK, sso.getCallback()); // code回调换取token
		registration.addInitParameter(SsoConstant.SSO_LOGOUT, sso.getLogout()); // 单点退出路径
		registration.addInitParameter(SsoConstant.SSO_USER_PATH, sso.getUserPath()); // 单点退出路径
		registration.addInitParameter(SsoConstant.PARAM_RESPONSE_TYPE, sso.getResponseType());
		registration.addInitParameter(SsoConstant.SSO_EXCLUDED_PATHS, sso.getExcludedPaths());
		// 租户
		registration.addInitParameter(SsoConstant.VAILD_TENANT, sso.getVaildTenant());
		registration.addInitParameter(SsoConstant.TENANT_KEY, sso.getTenantKey());
		
		return registration;
	}

}
